<?php

class Control_StuSearch extends QUI_Control_Abstract
{
    function render()
    {
        foreach ($_GET as $k=>$v) {
            $this->_view[$k] = $v;
        }
        $app = Q::registry('app');
		$user = $app->currentUser();

		$year_start = Q::ini('appini/year_start');
		$eyear_list = array();
		for($i = date('Y')+1;$i >= $year_start;$i--){
			$eyear_list[$i] = $i.'年';
		}
		$edu_sql = $edu_sql2 = "";
		if (!empty($user['school_id'])) {
			$edu_sql = " and id=".$user['school_id'];
			$edu_sql2 = " and school_id=".$user['school_id'];
		}
		$school_list = School::find('isdelete=0'.$edu_sql)->order("CONVERT( name USING gbk )")->getAll()->toHashMap("id","name");//院校
		$enroll_list = Enroll::find('isdelete=0 ')->order("CONVERT( name USING gbk ) desc")->getAll()->toHashMap("id","name");//批次
		$stage_list = Stage::find('isdelete=0')->order('CONVERT( name USING gbk )')->getAll()->toHashMap("id","name");//教育学段
		$this->_view['eyear_list']=$eyear_list;
		$this->_view['enroll_list']=$enroll_list;
		$this->_view['school_list']=$school_list;
		$this->_view['stage_list']=$stage_list;

        //page
        $this->_view['uri'] = preg_replace('/\/page\/\d+|\?.+/', '', QContext::instance()->requestURI());

        //export
        $this->_view['export'] = $this->get('export');
        $this->_view['btn_prama'] = $this->get('btn_prama');
        $exurl = $this->get('exurl');
        $exurl = $exurl ? $exurl : url('/export');
        $this->_view['exurl'] = $exurl . '?' . $_SERVER['QUERY_STRING'];
        $reseturl=$this->get('reseturl');
        $add=$this->get('add');
        $this->_view['add']=isset($add)?true : false;
        $this->_view['reseturl'] = empty($reseturl) ? url('') : $reseturl;
        $out = $this->_fetchView(dirname(__FILE__) . '/stusearch_view');

        return $out;
    }

    /*控件的过滤条件
     *@sql_where 传入的初始条件
     *@tbl 关联表 默认为空
     */
    public static function filterCond($sql_where, $tbl='') {
        if ($tbl) $tbl.='.';
        $app = Q::registry('app');
        $user = $app->currentUser();
        $context = QContext::instance();

        $qeyear = empty($context->qeyear)?$context->qeyear:trim($context->qeyear);
        $qschool = empty($context->qschool)?$context->qschool:trim($context->qschool);
        $qenroll = empty($context->qenroll)?$context->qenroll:trim($context->qenroll);
        $qstage = empty($context->qstage)?$context->qstage:trim($context->qstage);
        $qname = empty($context->qname)?$context->qname:trim($context->qname);
        $quserid = empty($context->quserid)?$context->quserid:trim($context->quserid);
        $qcid = empty($context->qcid)?$context->qcid:trim($context->qcid);
        if($qeyear!=null&&$qeyear!=""){
            $sql_where .= " and [{$tbl}entrance_year]=".$qeyear;
        }
        if($qschool!=null&&$qschool!=""){
            $sql_where .= " and [{$tbl}school_id]=".$qschool;
        }
        if($qenroll!=null&&$qenroll!=""){
            $sql_where .= " and [{$tbl}enroll_id]=".$qenroll;
        }
        if($qstage!=null&&$qstage!=""){
            $sql_where .= " and [{$tbl}stage_id]=".$qstage;
        }
        if($qname!=null&&$qname!=""){
            $sql_where .= " and [{$tbl}name] like '%".s($qname)."%'";
        }
        if($quserid!=null&&$quserid!=""){
            $sql_where .= " and [{$tbl}userid] like '%".s($quserid)."%'";
        }
        if($qcid!=null&&$qcid!=""){
            $sql_where .= " and [{$tbl}cid] like '%".s($qcid)."%'";
        }
        return $sql_where;
    }
}
